<template>
  <!-- 一条文章单元格 -->
 <div>
     <van-cell>
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{ obj.title }}</span>
        <img
          v-if="obj.cover.type === 1"
          :src="obj.cover.images"
          class="thumb"
        />
      </div>
      <!-- 有的图片没有imgags长度所以 -->
      <div class="thumb-box"  v-if="obj.cover.type > 1">
        <img class="thumb" :src="imgUrl" v-for="(imgUrl,index) in obj.cover.images" :key="index"/>
      </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>{{ obj.aut_name }}</span>
          <span>{{ obj.comm_count }}</span>
          <span>{{timeAgo(obj.pubdate)}}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon name="cross" @click.stop="crossBtnFn" v-show="isShow"/>
      </div>
    </template>
  </van-cell>
 </div>
</template>

<script>
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn' // import locale
import * as relativeTime from 'dayjs/plugin/relativeTime' // use locale
import { mapMutations } from 'vuex' // import 插件
dayjs.extend(relativeTime)
dayjs.locale('zh-cn')
export default {
  props: {
    obj: Object,
    isShow: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {

    }
  },
  created () {
    // console.log(this.obj)
  },
  methods: {
    timeAgo (targetTime) {
      // 格式化时间
      var a = dayjs()
      var b = dayjs(targetTime)
      const c = a.to(b) // 2 years ago
      // console.log(c)
      return c
    },
    ...mapMutations(['changeActionShowMutations', 'setarticlesArtIdMutations']),
    crossBtnFn () {
      this.changeActionShowMutations(true)
      this.setarticlesArtIdMutations(this.obj.art_id)
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
